package com.crazy.web.controller;

import com.crazy.model.major.dto.UserSession;
import com.crazy.util.IPUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zhaochaofeng
 * @version 1.0
 * @date 2020/11/19 9:21
 */
public abstract class BaseController {
    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;

    public BaseController() {
    }

    /**
     * 获取当前登录的user
     */
    public UserSession getUserSession() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Object object = subject.getPrincipal();
            if (object != null) {
                return (UserSession) object;
            }
        }
        return null;
    }

    /**
     * 判断是否拥有某个权限
     * @param role
     * @return
     */
    public boolean hasRole(String role) {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            return subject.hasRole(role);
        }else{
            return false;
        }
    }
}
